package com.irdstudio.apicenter.gateway.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import com.irdstudio.apicenter.gateway.comm.MaccsPacket;
import com.irdstudio.apicenter.gateway.comm.MaccsPacketUtil;
import com.irdstudio.apicenter.gateway.core.jdbc.MaccsBaseDAO;
import com.irdstudio.apicenter.gateway.util.spring.BeanUtils;
import com.irdstudio.apicenter.gateway.web.MaccsException;
import com.irdstudio.apicenter.gateway.web.MaccsTradeDefineVo;
import com.irdstudio.apicenter.gateway.web.view.JsonView;
import com.irdstudio.apicenter.gateway.web.view.View;

/**
 * 获取级联下拉菜单codelist。即：根据key值(ENNAME)由app_treedic表查询其子女（ABVENNAME=?）
 * @author 朱世伍
 * @version v1.0
 * @date 2015-10-20
 */
@Controller
public class AppTreeDicGetChildren implements IController {
	
	/* 文本日志输出 */
	private static final Logger logger = LoggerFactory
			.getLogger(AppTreeDicGetChildren.class);
	
	//下拉菜单codelist查询sql 
	private static final String queryTreeDicSql = " SELECT * FROM APP_TREEDIC T WHERE T.ABVENNAME = ? ";
	
	@Override
	public View execute(MaccsTradeDefineVo tradeDefine, MaccsPacket inPacket,
			HttpServletRequest request, HttpServletResponse response)
			throws MaccsException {
		
		//获取入参
		String enName = inPacket.getValue("enName");
		
		if(StringUtils.isEmpty(enName)){
			return new JsonView(request, response, MaccsPacketUtil
					.makeFailurePacket("未接收到参数传入值!"));
		}
		
		logger.debug("enName:" + enName);
		
		//获取级联下拉菜单
		MaccsBaseDAO jdbcDao = BeanUtils.getBean(MaccsBaseDAO.class);
		List<Map<String, Object>> treedic = jdbcDao.findMapList(queryTreeDicSql,
				new Object[] { enName });
		
		if (treedic.size() < 1) {
			return new JsonView(request, response, MaccsPacketUtil
					.makeFailurePacket("未初始化下拉菜单值!"));
		} else {
			MaccsPacket outPacket = MaccsPacketUtil
					.makeSimpleSuccessPacket("OK!");
			outPacket.addVariable("treedic", treedic);
			return new JsonView(request, response, outPacket);
		}	
		
	}

}
